package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/12/10.
 * common problem solver template.
 */
public class LC2273 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<String> removeAnagrams(String[] words) {
            List<String> ans = new ArrayList<>();
            for (int i = words.length - 1; i >= 0; i--) {
                if (i > 0 && words[i].length() == words[i - 1].length() && match(words[i], words[i - 1])) continue;
                ans.add(words[i]);
            }

            // 反转lists
            Collections.reverse(ans);
            return ans;
        }

        public boolean match(String a, String b) {
            int[] cnt1 = new int[26], cnt2 = new int[26];
            for (int i = 0; i < a.length(); i++) {
                cnt1[a.charAt(i) - 'a']++;
            }
            for (int i = 0; i < b.length(); i++) {
                cnt2[b.charAt(i) - 'a']++;
            }
            return Arrays.toString(cnt1).equals(Arrays.toString(cnt2));
        }
    }

    public void run() {
        System.out.println(new Solution().removeAnagrams(new String[]{"abba", "baba", "bbaa", "cd", "cd"}));
        System.out.println(new Solution().removeAnagrams(new String[]{"a","b","c","d","e"}));
    }

    public static void main(String[] args) throws Exception {
        LC2273 an = new LC2273();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
